﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;

namespace WeixinSDK.API
{
    /// <summary>
    /// 生成带参数的二维码
    /// </summary>
    public static class QRCode
    {
        public static Ticket Create(string access_token, string jsonData)
        {
            var json = WxApiHelper.HttpPost(access_token, WxApiHelper.Categroy.qrcode, WxApiHelper.Verb.create, jsonData);
            return WxJson.DeserializeObject<Ticket>(json);
        }

        /// <summary>
        /// 创建临时二维码ticket
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="expire_seconds">该二维码有效时间，以秒为单位。 最大不超过1800。</param>
        /// <param name="scene_id">场景值ID，临时二维码时为32位非0整型</param>
        /// <returns></returns>
        public static Ticket CreateTemp(string access_token, int expire_seconds, int scene_id)
        {
            JObject obj = new JObject() { 
                {"expire_seconds",expire_seconds},
                {"action_name","QR_SCENE"},
                {"scene",new JObject(){"scene_id",scene_id}}
            };
            return Create(access_token, obj.ToString());
        }

        /// <summary>
        /// 创建永久二维码ticket
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="scene_id">场景值ID，永久二维码时最大值为100000（目前参数只支持1--100000）</param>
        /// <returns></returns>
        public static Ticket CreatePermanet(string access_token, int scene_id)
        {
            JObject obj = new JObject() { 
                {"action_name","QR_LIMIT_SCENE"},
                {"scene",new JObject(){"scene_id",scene_id}}
            };
            return Create(access_token, obj.ToString());
        }

        public class Ticket : WxError
        {
            /// <summary>
            /// 获取的二维码ticket，凭借此ticket可以在有效时间内换取二维码。
            /// </summary>
            public string ticket { get; set; }
            /// <summary>
            /// 二维码的有效时间，以秒为单位。最大不超过1800。
            /// </summary>
            public int expire_seconds { get; set; }
        }

        //public static bool GetImageByTicket(string ticket, out byte[] imageBytes)
        //{

        //}
    }
}
